<?php
require_once('functions.php');

function getSuppliers()
{
        $conn = getConnection();
        return $conn->query('SELECT * FROM Part_Supplier');
}

function GetSupplier($id)
{
        $conn = getConnection();
        $results = $conn->query("SELECT * FROM Part_Supplier WHERE supplier_ID=$id");
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}

function blankSupplier()
{
	return array('supplier_ID' => null, 'item' => null, 'company' => null);
}

function deleteSupplier($id)
{
		$conn = getConnection();
		$conn->query("DELETE FROM Part_Supplier WHERE supplier_ID = '$id'");
		$conn->close();
		header('Location: index.php');
}

function saveSupplier(&$row)

{
        $conn = getConnection();
        $row2 = escapeRow($row, $conn);
        
        if(!empty($row['supplier_ID']))
        	$sql =  "UPDATE Part_Supplier SET item='$row2[item]', company='$row2[company]' WHERE supplier_ID='$row2[supplier_ID]'";
       	else
       		$sql = "INSERT INTO Part_Supplier VALUES (NULL,'$row2[item]', '$row2[company]')";
       		
        $results = $conn->query($sql);
        $error = $conn->error;
        
        if(empty($row['supplier_ID']) && empty($error))
        {
                $row['supplier_ID'] = $conn->insert_id;
        }

        $conn->close();
       
        return $error == '' ? true : array('SQL Error' => $error);
}
